/**
 * 
 */
package co.edu.eam.iso.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author Giovanny Ramirez Blandon
 * @version 1.0
 */
public class BigDecimalUtil {
	
	private static final RoundingMode DEFAULT_ROUNING_MODE = RoundingMode.HALF_UP;
	private static final int DEFAULT_SCALE = 2;

	public static final BigDecimal FIVE = new BigDecimal(5);
	public static final BigDecimal HUNDRED = new BigDecimal(100);

	public static BigDecimal wrap(double value) {
		return new BigDecimal(value);
	}

	public static BigDecimal ruleOfThree(BigDecimal a, BigDecimal b,
			BigDecimal y) {
		BigDecimal x = divide(multiply(a, y), b);
		return x;
	}
	
	public static boolean between(BigDecimal value, BigDecimal minValue,
			BigDecimal maxValue) {
		return value.compareTo(minValue) >= 0 && value.compareTo(maxValue) <= 0;
	}
	
	public static BigDecimal add(BigDecimal... values) {
		BigDecimal result = BigDecimal.ZERO;
		for (BigDecimal value : values) {
			result = round(result.add(value), DEFAULT_SCALE, DEFAULT_ROUNING_MODE);
		}
		return result;
	}
	
	public static BigDecimal substract(BigDecimal value, BigDecimal subtrahend) {
		return value.subtract(subtrahend);
	}

	public static BigDecimal divide(BigDecimal value, BigDecimal divisor) {
		int scale = Math.max(value.scale(), divisor.scale());
		scale = Math.max(scale, DEFAULT_SCALE);

		return value.divide(divisor, scale, DEFAULT_ROUNING_MODE);
	}

	public static BigDecimal multiply(BigDecimal value, BigDecimal multiplicand) {
		return round(value.multiply(multiplicand), DEFAULT_SCALE, DEFAULT_ROUNING_MODE);
	}

	public static BigDecimal round(BigDecimal value, int scale,
			RoundingMode roundingMode) {
		return value.setScale(scale, roundingMode);
	}

	public static BigDecimal difference(BigDecimal value) {
		BigDecimal difference = substract(value, HUNDRED);
		return add(value, difference);
	}
}
